﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace FluentResourceMapping
{
    /// <summary>
    /// Resposible for storing the resource mapping.
    /// </summary>
    public class TypeResourceMapping
    {
        public Type MappingType { get; private set; }

        public TypeResourceMapping(Type mappingType)
        {
            MemberResourceMappings = new List<MemberResourceMapping>();
            MappingType = mappingType;
        }

        public IList<MemberResourceMapping> MemberResourceMappings { get; private set; }

        public string GetResource(string propertyName)
        {
            var memberMapping = MemberResourceMappings.FirstOrDefault(o => o.Member.Name == propertyName);
            if (memberMapping != null)
                return memberMapping.Value;
            return null;
        }

        public string GetResourceFor<T>(Expression<Func<T, object>> memberExpression)
        {
            //TODO: Improve this method

            dynamic strange = memberExpression;
            string name = strange.Body.Member.Name;

            return GetResource(name);
        }
    }
}